// mypage/addBady/addBaby.js
var app=getApp();
var other=require("../../utils/other");
var util=require("../../utils/util");
var config=require("../../api/config");
var my=require("../../api/my")


Page({
  /*页面的初始数据*/
  data: {
    uid: '',
    sex_list:[1,2],
    sex_index: 3,
    className:'',
    show_check: false,
    baby_img: '',
    name: '',
    age: '',
    age_step: '',
    grade: '',
    title_index: 0,
    item_index: 0,
    pop_show: true,
    show_com: 0,
    grade_list: [],
    com_data: [],
    add: true,
    default_img: ["http://ossdev0.jiabala.com/tnr5Y8kcKzNdR","http://ossdev0.jiabala.com/jjDPpzQfmThK3"],
    token: '',
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this;
    var uid=options.id;
    that.setData({uid: uid,token:wx.getStorageSync('token')})
    //
    that.get_grade_list()
  },
  // 选择性别
  check_sex:function(e){
    var that = this,default_img=that.data.default_img;
    var index = e.currentTarget.dataset.index;
    if(index==1){
      that.setData({className: 'active'})
    }else{
      that.setData({className: 'active_else'})
    }
    that.setData({sex_index:index,baby_img:default_img[index]})
    setTimeout(function(){that.setData({show_check: true})},300)
  },
  // 上传头像
  choose_image:function(e){
    var that = this;
    wx.chooseImage({
      count: 1,
      sourceType: ['album', 'camera'],
      success: (res) => {
        // console.log(res)
        wx.showLoading({
          title: '上传中'
        })
        that.get_oss(res)
      }
    })
  },
  // 名字
  input_name:function(e){
    var that=this;
    var value=e.detail.value;
    if(value.length>10){
      util.page_show_toast('宝宝昵称不能超过10个字符',1500)
    }
    that.setData({name:value.substring(0,10)});
  },
  // 生日和在读年纪
  open_addpop:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    if(index==2){
      var obj = other.dateTimePicker(2000,new Date().getFullYear())
      that.setData({com_data: obj.dateTimeArray,pop_show: !that.data.pop_show,show_com: index})
    }else if(index==3){
      that.setData({com_data:that.data.grade_list,pop_show: !that.data.pop_show,show_com: index})
    }
  },
  close_addpop:function(e){
    var that = this;
    var obj = e.detail.pop_show;
    that.setData({pop_show: obj})
  },
  change_time:function(e){
    this.setData({com_data: e.detail.time})
  },
  confirm_age:function(e){
    var that = this;
    that.setData({age: e.detail.age})
  },
  // 选择年级
  check_inst:function(e){
    var that = this;
    var info=e.detail.data;
    that.setData({title_index:info.title_index,item_index:info.item_index})
  },
  confirm_inst:function(e){
    var that = this;
    that.setData({grade: e.detail.item})
  },
  confirm_baby:function(){
    var that=this,sex_list=that.data.sex_list,index=that.data.sex_index,baby_img=that.data.baby_img,name=that.data.name,age=that.data.age,
    grade=that.data.grade,uid=that.data.uid,testSpace = /^[ ]*$/g,add=that.data.add;
    if(testSpace.test(name)||!name){
      util.page_show_toast('请输入宝宝昵称',1500)
      return false;
    }
    if(!age){
      util.page_show_toast('请选择宝宝生日',1500)
      return false;
    }
    if(!grade){
      util.page_show_toast('请选择宝宝在读年级',1500)
      return false;
    }
    var params={
      "name": util.removeAllSpace(name),
      "birthday": Date.parse(age),
      "sex": sex_list[index],
      "parentId": uid,
      "avatar": baby_img,
      "grade": grade
    }
    if(add){
      that.add_baby(params);
    }
  },
  // 新增宝宝
  add_baby:function(params){
    var that=this,token=that.data.token;
    that.setData({add: false})
    my.add_baby(params,token).then(res=>{
      if(res.data.code==200){
        app.globalData.change_baby=true;
        app.globalData.change_table=true;
        util.page_show_toast('新增成功',1500)
        setTimeout(function(){
          that.setData({add: true})
          wx.navigateBack({
            delta: 1
          });
        },1500)
      }
    })
  },
  // 获取在读年纪列表
  get_grade_list:function(){
    var that=this;
    config.get_grade_list().then(res=>{
      if(res.data.code==200){
        var list=res.data.data;
        that.setData({grade_list: list})
      }
    })
  },
  // 获取oss上传图片
  get_oss:function(file){
    var that=this,randomStr=util.randomString(13);
    var params={
      "isPublic": true,
      "isHttps": true
    }
    config.get_oss(params).then(res=>{
      if(res.data.code==200){
        var info=res.data.data;
        wx.uploadFile({
          url: info.host,
          filePath: file.tempFilePaths[0],
          name: 'file',
          formData: {
            "key": randomStr,
            "policy": info.policy,
            "OSSAccessKeyId": info.accessKeyId,
            "signature": info.signature,
            "success_action_status": '200'
          },
          success:function(){
            wx.hideLoading()
            var baby_img=info.host+'/'+randomStr;
            that.setData({baby_img: baby_img})
          },
        })
      }
    })
  },
  onReady:function(){
    this.dialog = this.selectComponent('#addpop');
  },
})